/***************************************************************************************
 * SAGE - Scalable Adaptive Graphics Environment
 *
 * Module:  audioCircBuf.h
 * Author : Byungil Jeong, Hyejung Hur
 *
 *	Description: This is the header file for the circular buffer of sage pixel blocks
 *
 *	Notes	:	This class is supposed to provide the common buffers for holding 
 *			the pixel blocks. It also takes care of the reading and writing of pixel blocks 
 *			in a circular fashion. The member functions will be able to provide a complete 
 *			interface to handling these buffers. 
 *			
 *			The algo is that the buffers are writen to and read from in the same direction 
 *			[0 to n and then back to 0]
 *			
 *			
 * Copyright (C) 2004 Electronic Visualization Laboratory,
 * University of Illinois at Chicago
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either Version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser Public License along
 * with this library; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * Direct questions, comments etc about SAGE to bijeong@evl.uic.edu or 
 * http://www.evl.uic.edu/cavern/forum/
 *
*****************************************************************************************/


#ifndef _AUDIO_CIRCBUF_H
#define _AUDIO_CIRCBUF_H

#include "sageBase.h"

class sageSynchronizer;

class audioBlock {
public:
	long frameIndex;
	long gframeIndex;
	int reformatted;
	void* buff;
	double timestamp;
	
	audioBlock(): frameIndex(-1), gframeIndex(-1), reformatted(0), buff(NULL), timestamp(0) {};
	~audioBlock() {};
};

/** todo */
// need to extend for several readers and writers....

class audioCircBuf {
private:
	int audioId;
	
	int readIndex;
	int writeIndex;
	
	/** numbers of audio block
	*/
	int blocksNum;
	
	/** audio format : float, short, char, unsigned char
	*/
	sageSampleFmt sampleFmt;
	
	/** buffer size in an audio block
	*/
	int sampleBuffSize;
	
	/** array of audio block
	*/
	audioBlock* blockArray;
	
	/** bytes in an audio block
	*/
	int bytesBlock;
	
	bool full;
	bool empty;
	
	/** synchronizer for audio and video streaming
	*/
	sageSynchronizer* synchronizer;
	
	/** make buffer lock- can not write 
	*/
	bool locked;
	
	/** frame index of last block in writing
	*/
	int lastFrameIndex;
	
	int refRead;
	int refMutex;	
	std::vector<int> readers;

protected:
	void clearBlocks();
	
public:
	audioCircBuf();
	~audioCircBuf();
	
	/** create aduio blocks and create buffers in each audio block.
	*/	
	int init(int id, int blockNum, sageSampleFmt fmt, int size);	
	
	/** get audio block pointer for writing 
	*/
	audioBlock* getNextWriteBlock(); 

	/** todo */ 
	// not good idea to give pointer for reading
	/** get audio block pointer for read
	*/
	audioBlock* readBlock(int Id = 0);
	audioBlock* readBlock(int Id, int frameNum);

	/** update read index
	*/
	int updateReadIndex();
	
	/** update write index
	*/
	int updateWriteIndex();
	
	int getReadIndex();
	int getWriteIndex();
	
	/** get numbers of audio blocks
	*/
	int getSize();
	int getAudioId();
	int getBytesBlock();
		
	void clearBlock(int frameNum);
	void reset();
	
	void connectSync(sageSynchronizer* synch);
	
	/** use for synchronization 
	*/
	void setLock();
	void setFree();
	
	/** get frame number of recently recored block 
	*/
	int getLastFrameIdx();

	// add
	int addReader();
	void deleteReader(int Id);
};

#endif
